{% extends "repo/repo.html" %}

{% block container_class_name %}repo{% endblock %}

{% block subcontainer %}
<div class="compare inner bubble">
    <header class="header">
        {% if is_repo_member %}
        <div id="mergePullrequest" class="pull-right hide">
            <a id="merge" data-original-title=" 直接合并分支代码" rel="tooltip" href="javascript: void(0)" class="btn btn-success">合并分支代码</a>
            <a data-original-title=" 出于礼貌，提前合并目标分支代码，减少对方合并时出现冲突的可能性" rel="tooltip" href="/{{user_name}}/{{repo_name}}/pull/new/{{user_name}}:{{from_refs}}...{{user_name}}:{{to_refs}}/" class="btn">创建合并请求</a>
        </div>
        {% endif %}
    </header>

  {# compare #}
  <h2 class="heading">比较</h2>
  <div class="repo-refs colspan">
    <div class="ref span">
      <select id="sourceRefs" class="select2">
        {% for branch in refs_meta.branches %}
          <option value="{{ branch }}" {%if branch == from_refs %} selected="selected" {%endif%}>{{ branch }}</option>
        {% endfor %}
        {% if from_refs not in refs_meta.branches%}
          <option value="{{ from_refs }}" selected="selected">{{ from_refs }}</option>
        {% endif %}
      </select>
    </div>
    <div class="span chevron"><i class="icon-random"></i></div>
    <div class="ref span">
        <select id="descRefs">
            {% for branch in refs_meta.branches %}
            <option value="{{ branch }}" {%if branch == to_refs %} selected="selected" {%endif%}>{{ branch }}</option>
            {% endfor %}
            {% if to_refs not in refs_meta.branches%}
            <option value="{{ to_refs }}" selected="selected">{{ to_refs }}</option>
            {% endif %}
        </select>
    </div>
  </div>

  <div id="commitsDiff"></div>

  <div id="mergeResult" class="hide">
    <pre id="mergeOutput" class="shell"></pre>
    <a id="mergeAfter" href="/{{user_name}}/{{repo_name}}/refs/graph/{{to_refs}}/" class="btn btn-primary pull-right">确定</a>
  </div>
</div>
{% endblock %}
{% block subjs %}
<script>
  $(function(){
    var global_repo_branch_map = {};
    $('#mergePullrequest').tooltip({
      selector: "a[rel=tooltip]"
    });
    $('#sourceRefs, #descRefs').change(function(){
      var source_refs = $('#sourceRefs').attr('value');
      var desc_refs = $('#descRefs').attr('value');
      window.location.href = '/{{ user_name }}/{{ repo_name }}/compare/' + source_refs + '...' + desc_refs + '/';
    });
    var repoComparer = new RepoComparer('{{user_name}}', '{{repo_name}}', '{{from_refs}}', '{{to_refs}}');
    repoComparer.renderCompare($('#commitsDiff'), function(json){
      if(json.allow_merge) {
        $('#mergePullrequest').show();
      }
    });
    $('#merge').click(function(){
      $('#commitsDiff').hide();
      $('#mergeResult').show();
      $('#mergeOutput').show();
      var i = 0;
      var interval = setInterval(function() {
        i = i + 1;
        var j = i % 4;
        var html = '正在进行 Merge，请耐心等候'
        for (var k = 0; k < j; k++) {
          html = html + '.'
        }
      $('#mergeOutput').html(html);
      }, 500);
      $('html, body').animate({ scrollTop: 0 }, 'fast');
      $.post('/{{user_name}}/{{repo_name}}/merge/{{to_refs}}...{{from_refs}}/', {csrfmiddlewaretoken: '{{ csrf_token }}'}, function(json){
        if(json.result == 'success') {
          clearInterval(interval);
          $('#mergeOutput').html(json.output);
          if(json.returncode != 0) {
            $('#mergeAfter').attr('href', '/{{user_name}}/{{repo_name}}/compare/{{from_refs}}...{{to_refs}}/');
            $('#mergeAfter > button').removeClass('btn-primary').addClass('btn-danger').text('合并失败，请检查是否 存在冲突 或者 non-fast-forward');
          }
          $('#mergeAfter').show();
        }
      });
      return false;
    });
  })
</script>
{% endblock %}


